package com.swss.g.sys.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import com.swss.g.base.impl.BaseServiceImpl;
import com.swss.g.sys.bean.GBCode;
import com.swss.g.sys.bean.GMk;
import com.swss.g.sys.bean.Nation;
import com.swss.g.sys.bean.Storage;
import com.swss.g.sys.dao.DataBaseDAO;
import com.swss.g.sys.dao.GBCodeDAO;
import com.swss.g.sys.dao.GmkDAO;
import com.swss.g.sys.dao.NationDAO;
import com.swss.g.sys.dao.StorageDAO;
import com.swss.g.sys.service.SysService;
import com.swss.util.DoDate;
import com.swss.util.TreeNode;

public class SysServiceImpl extends BaseServiceImpl<GMk> implements SysService {

	private GmkDAO gmkDao;
	private GBCodeDAO gbCodeDao;
	private DataBaseDAO dataBaseDao;
	private NationDAO nationDao;
	private StorageDAO storageDao;
	static Logger log = Logger.getLogger(SysServiceImpl.class);

	// 查询字典
	public List findSomeField(String FieldName) throws Exception {
		// TODO Auto-generated method stub
		return this.gmkDao.findSomeField(FieldName);

	}

	public GmkDAO getGmkDao() {
		return gmkDao;
	}

	public void setGmkDao(GmkDAO gmkDao) {
		this.setBaseDao(gmkDao);
		this.gmkDao = gmkDao;
	}

	public List getFundSub() throws Exception {
		// TODO Auto-generated method stub
		return this.gmkDao.findSomeField("经费来源");
	}

	public List getAstSource() throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomeField("资产来源");
	}

	public List getNowStatus() throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomeField("现状");
	}

	public List getUseDir() throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomeField("使用方向");
	}

	private List<TreeNode> getGBRoot() throws Exception {
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		TreeNode tn = new TreeNode();
		tn.setId("r01");
		tn.setText("一、土地房屋及构筑物");
		tn.setValue("1");
		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r02");
		tn.setText("二、通用设备");
		tn.setValue("1");
		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r03");
		tn.setText("三、专用设备");
		tn.setValue("1");
		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r04");
		tn.setText("四、交通运输设备");

		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r05");
		tn.setText("五、无电气设备");

		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r06");
		tn.setText("六、电子产品及通讯设备");

		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r07");
		tn.setText("七、仪器仪表、计量标准器具及量具、衡器");

		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r08");
		tn.setText("八、文艺体育设备");

		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r09");
		tn.setText("九、图书、文物及陈列品");

		tn.setHasChildren(true);
		treeNodeList.add(tn);

		tn = new TreeNode();
		tn.setId("r10");
		tn.setText("十、家具、用具及其它类");
		tn.setHasChildren(true);
		treeNodeList.add(tn);

		return treeNodeList;

	}

	public String findGBCodeJSONByNo(String root) throws Exception {
		
		// TODO Auto-generated method stub
		List<GBCode> gbList = null;
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		TreeNode tn = null;

		if (root.trim().equals("source")) {
			treeNodeList = this.getGBRoot();
		} else {
			if (root.indexOf("r") == -1) {
				
				gbList = this.gbCodeDao.findGBCodeChildByNo(root);
			} else {
				//System.out.println(root.substring(1));
				gbList = this.gbCodeDao.findGBCodeBySort10(root.substring(1));
			}
			int len = 0;

			for (GBCode gb : gbList) {
				len = gb.getAscNo().trim().length();

				tn = new TreeNode();
				tn.setId(gb.getAscNo().trim());
				tn.setText("(" + gb.getAscNo().trim() + ")"
						+ gb.getAscName().trim());
				tn.setValue(gb.getAscNo().trim() + "-" + gb.getAscName().trim()
						+ "-" + gb.getAscSort().trim() + "-"
						+ gb.getScrapNo().trim() + "-"
						+ gb.getMareunit().trim());
				if (len != 6) {
					tn.setHasChildren(true);
				} else {

					tn.setHasChildren(false);
				}
				treeNodeList.add(tn);

				if (len == 4) {
					break;
				}

			}
		}

		return JSONArray.fromObject(treeNodeList).toString();
	}

	public String searchNation(String keyword) {
		Pattern p = Pattern
				.compile("^-?(?:\\d+|\\d{1,3}(?:,\\d{3})+)(?:\\.\\d+)?$");

		Matcher m = p.matcher(keyword);
		Nation nation = new Nation();
		if (m.matches()) {
			nation.setGbm(keyword);
		} else {
			nation.setGb(keyword);
		}

		try {

			return JSONArray.fromObject(
					this.nationDao.findNationLikeBean(nation)).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("查询国别码错误");
			return "";
		}
	}

	public GBCodeDAO getGbCodeDao() {
		return gbCodeDao;
	}

	public void setGbCodeDao(GBCodeDAO gbCodeDao) {
		this.gbCodeDao = gbCodeDao;
	}

	public String findGBJSONByKeyword(String keyword) throws Exception {
		// TODO Auto-generated method stub
		// /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/

		Pattern p = Pattern
				.compile("^-?(?:\\d+|\\d{1,3}(?:,\\d{3})+)(?:\\.\\d+)?$");
		Matcher m = p.matcher(keyword);
		GBCode gbCode = new GBCode();
		if (m.matches()) {
			gbCode.setAscNo(keyword);
		} else {
			gbCode.setAscName(keyword);
		}

		return JSONArray.fromObject(this.gbCodeDao.findGBCodeLikeBean(gbCode))
				.toString();
	}

	public List getChargeOffDept() throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomeField("拟核销财务科室");
	}

	public String[] dbFileNames(String url) throws Exception {
		// TODO Auto-generated method stub
		return new File(url).list();
	}

	public int backUpDB(String url) throws Exception {
		// TODO Auto-generated method stub
		DoDate doDate = new DoDate();
		String fileName = "\\" + doDate.formatDateFileName(new Date()) + ".dat";
		this.dataBaseDao.backupDB(url, fileName);
		return 1;
	}

	public DataBaseDAO getDataBaseDao() {
		return dataBaseDao;
	}

	public void setDataBaseDao(DataBaseDAO dataBaseDao) {
		this.dataBaseDao = dataBaseDao;
	}

	public int deleteDB(String fileName, String url) throws Exception {
		// TODO Auto-generated method stub
		fileName = url + "\\" + fileName;
		File file = new File(fileName);
		file.delete();
		return 1;

	}

	public int restoreDB(String url, String fileName) throws Exception {
		// TODO Auto-generated method stub
		this.dataBaseDao.restoreDB(url, "\\" + fileName);
		return 1;

	}

	public NationDAO getNationDao() {
		return nationDao;
	}

	public void setNationDao(NationDAO nationDao) {
		this.nationDao = nationDao;
	}

	public String searchStorage(String keyword,String deptNo) throws Exception {
		// TODO Auto-generated method stub
		Pattern p = Pattern
				.compile("^-?(?:\\d+|\\d{1,3}(?:,\\d{3})+)(?:\\.\\d+)?$");

		Matcher m = p.matcher(keyword);
		Storage storage = new Storage();
		if (m.matches()) {
			storage.setSsNo(keyword);
		} else {
			storage.setSsName(keyword);
		}
		storage.setDeptNo(deptNo);

		try {
			return JSONArray.fromObject(
					this.storageDao.findStorageLikeBean(storage)).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("查询存放地点错误");
			return "";
		}
	}

	public StorageDAO getStorageDao() {
		return storageDao;
	}

	public void setStorageDao(StorageDAO storageDao) {
		this.storageDao = storageDao;
	}

	public List getFinanceUser(String finUser) throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomField("财务账户",finUser);
	}

	public List getZheJiuFangShi() throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomeField("折旧方式");
	}
    public List getApplyMethod() throws Exception{
    	return this.getGmkDao().findSomField("现状","1");
    }

	public List getApplyResult() throws Exception {
		// TODO Auto-generated method stub
		return this.getGmkDao().findSomeField("处理结果");
	}

	public List<?> getClbz() throws Exception {
		// TODO Auto-generated method stub
		return this.gmkDao.findSomeField("车辆编制");
	}
}
